<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="企业名称" prop="enterpriseName">
        <el-input
          size="small"
          v-model="queryParams.enterpriseName"
          placeholder="请输入企业"
          clearable>
        </el-input>
      </el-form-item>
      <!--<el-form-item label="开始时间" prop="beginTime">-->
        <!--<el-date-picker clearable size="small"-->
          <!--v-model="queryParams.beginTime"-->
          <!--type="date"-->
          <!--value-format="yyyy-MM-dd"-->
          <!--placeholder="选择开始时间">-->
        <!--</el-date-picker>-->
      <!--</el-form-item>-->
      <!--<el-form-item label="结束时间" prop="endTime">-->
        <!--<el-date-picker clearable size="small"-->
          <!--v-model="queryParams.endTime"-->
          <!--type="date"-->
          <!--value-format="yyyy-MM-dd"-->
          <!--placeholder="选择结束时间">-->
        <!--</el-date-picker>-->
      <!--</el-form-item>-->
      <el-form-item label="租赁模式" prop="leaseModel">
        <el-select clearable v-model="queryParams.leaseModel"
                   placeholder="请选择租赁模式">
          <el-option label="按间" value="按间"></el-option>
          <el-option label="按面积" value="按面积"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="收费类型" prop="feeModel">
        <el-select clearable v-model="queryParams.feeModel"
                   placeholder="请选择收费类型">
          <el-option label="按月" value="按月"></el-option>
          <el-option label="按季度" value="按季度"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['life:feApartment:add']"
        >公寓出租</el-button>
      </el-col>
      <!--<el-col :span="1.5">-->
        <!--<el-button-->
          <!--type="success"-->
          <!--plain-->
          <!--icon="el-icon-edit"-->
          <!--size="mini"-->
          <!--:disabled="single"-->
          <!--@click="handleUpdate"-->
          <!--v-hasPermi="['life:feApartment:edit']"-->
        <!--&gt;修改</el-button>-->
      <!--</el-col>-->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['life:feApartment:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleImport"
          v-hasPermi="['life:feApartment:export']"
        >导入</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['life:feApartment:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="feApartmentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="企业名称" align="center" prop="enterpriseName" />
      <el-table-column label="开始时间" align="center" prop="beginTime" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.beginTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="endTime" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="免租开始时间" align="center" prop="rentFreeBeginDate" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.rentFreeBeginDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="免租结束时间" align="center" prop="rentFreeEndDate" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.rentFreeEndDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>-->
      <el-table-column label="租赁模式" align="center" prop="leaseModel" width="80">
      </el-table-column>
      <el-table-column label="收费类型" align="center" prop="feeModel" width="80">
        <!--<template slot-scope="scope">-->
          <!--<span>{{scope.row.feeModel == '0'? '按月' : scope.row.feeModel == '1'? '按季度': '暂无'}}</span>-->
        <!--</template>-->
      </el-table-column>
      <el-table-column label="下期账单日期" align="center" prop="nextPropertyDate" width="120">
      </el-table-column>
      <!--<el-table-column label="保证金" align="center" prop="surety" width="80"/>-->
      <!--<el-table-column label="租金" align="center" prop="rental" width="80" />-->
      <el-table-column label="合同面积(m²)" align="center" prop="contractArea" width="100" />
      <el-table-column label="面积单价(元)" align="center" prop="areaPrice" width="100" />
      <el-table-column label="状态" align="center" prop="recordState" width="80">
        <!--<template slot-scope="scope">-->
          <!--<span>{{scope.row.recordState == '0'? '未到期' : scope.row.recordState == '1'? '已到期': '暂无'}}</span>-->
        <!--</template>-->
      </el-table-column>
      <el-table-column label="是否生效" align="center" prop="effect" width="80">
        <!--<template slot-scope="scope">-->
          <!--<span>{{scope.row.effect == '0'? '未生效' : scope.row.effect == '1'? '已生效': '暂无'}}</span>-->
        <!--</template>-->
      </el-table-column>
      <!--<el-table-column label="计算方式" align="center" prop="calculationMode" width="100" />-->
      <!--<el-table-column label="备注" align="center" prop="remark" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['life:feApartment:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleEffect(scope.row)"
            v-hasPermi="['life:feApartment:edit']"
          >状态修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleDetail(scope.row)"
            v-hasPermi="['life:feApartment:edit']"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['life:feApartment:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 抄表导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">
          将文件拖到此处，或
          <em>点击上传</em>
        </div>
        <div class="el-upload__tip" slot="tip">
          <!--<el-checkbox v-model="upload.updateSupport" />是否更新已经存在的数据-->
          <el-link type="info" style="font-size:12px" @click="importTemplate">下载模板</el-link>
        </div>
        <div class="el-upload__tip" style="color:red" slot="tip">提示：仅允许导入“xls”或“xlsx”格式文件！</div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改公寓租赁记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="520px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="企业名称" prop="enterpriseId">
            <el-select disabled
              size="small"
              v-model="form.enterpriseId"
              filterable
              placeholder="请选择企业"
              clearable>
              <el-option
                v-for="item in zsCustomerList"
                :key="item.id"
                :label="item.enterpriseName"
                :value="item.id">
              </el-option>
            </el-select>
        </el-form-item>

        <el-form-item label="收费类型" prop="feeModel">
          <el-select clearable v-model="form.feeModel"
                     placeholder="请选择收费类型">
            <el-option label="按月" value="按月"></el-option>
            <el-option label="按季度" value="按季度"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="租赁生效状态" prop="effect">
          <el-select clearable v-model="form.effect"
            placeholder="请选择租赁状态">
            <el-option label="未生效" value='未生效'></el-option>
            <el-option label="已生效" value='已生效'></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="租赁计算方式" prop="calculationMode">
          <el-select
            clearable
            v-model="form.calculationMode"
            placeholder="请选择租赁计算方式">
            <el-option label="手动计费" value="手动计费"></el-option>
            <el-option label="自动计费" value="自动计费"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listFeApartment, getFeApartment, delFeApartment, addFeApartment, updateFeApartment,addFeApartment1 } from "@/api/smartParks/operatingFee/apartment";
import { listQyEnterprise } from '@/api/smartParks/merchants/merchantsCompanySettlein';
import { getToken } from "@/utils/auth";
export default {
  name: "FeApartment",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 公寓租赁记录表格数据
      feApartmentList: [],
      zsCustomerList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 用户导入参数
      upload: {
        // 是否显示弹出层
        open: false,
        // 弹出层标题
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/life/feApartment/importData"
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        enterpriseName: null,
        beginTime: null,
        endTime: null,
        leaseModel: null,
        feeModel: null,
        surety: null,
        contractArea: null,
        rental: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        delFlag: [
          { required: true, message: "删除标志(0正常 1删除)不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    listQyEnterprise().then((response) => {
      this.zsCustomerList = response.rows
    })
  },
  methods: {
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "抄表导入";
      this.upload.open = true;
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert(response.msg, "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download('life/feApartment/importTemplate',{
        ...this.queryParams
      }, `公寓租赁模板.xlsx`)
    },
    /** 查询公寓租赁记录列表 */
    getList() {
      this.loading = true;
      listFeApartment(this.queryParams).then(response => {
        this.feApartmentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        enterpriseId: null,
        beginTime: null,
        endTime: null,
        rentFreeBeginDate: null,
        rentFreeEndDate: null,
        leaseModel: null,
        feeModel: null,
        surety: null,
        contractArea: null,
        rental: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$router.push({ name: "PartmentAdd" });
    },
    handleEffect(row) {
      this.reset()
      const id = row.id || this.ids
      getFeApartment(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改租赁状态'
      })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      // const id = row.id || this.ids
      // getFeApartment(id).then(response => {
      //   this.form = response.data;
      //   this.open = true;
      //   this.title = "修改公寓租赁记录";
      // });
      const id = row.id || this.ids;
      //, name: row.enterpriseName
      this.$router.push({
        name: 'QypartmentAdd',
        query: { id: row.id, name: row.enterpriseName ,disabled:false},
      });
    },
    handleDetail(row){
      const id = row.id || this.ids
      //console.log("feApartment handleDetail " + id +" ," + row.enterpriseName);
      this.$router.push({
        name: 'QypartmentAdd',
        query: { id: row.id, name: row.enterpriseName,disabled:true },
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateFeApartment(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addFeApartment(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除公寓租赁记录编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delFeApartment(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('life/feApartment/export', {
        ...this.queryParams
      }, `life_feApartment.xlsx`)
    }
  }
};
</script>
